package com.reggie.module.entity.dto.dishDto;


import com.reggie.module.entity.po.Dish;
import com.reggie.module.entity.po.DishFlavor;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static com.reggie.common.Constant.LOGIN_SESSION;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class DishDto extends Dish {

    private List<DishFlavor> flavors = new ArrayList<>();

    private String categoryName;

    private Integer copies;


    public Dish changeToDish(HttpServletRequest request) {
        Dish dish = new Dish();
        dish.setName(this.getName());
        dish.setCategoryId(this.getCategoryId());
        dish.setPrice(this.getPrice());
        dish.setCode("");
        dish.setImage(this.getImage());
        dish.setDescription(this.getDescription());
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        Long empId = (Long) request.getSession().getAttribute(LOGIN_SESSION);
        dish.setCreateUser(empId);
        dish.setUpdateUser(empId);
        return dish;
    }

    public void changeDishFlavor(Long dishId, HttpServletRequest request) {
        for (int i = 0; i < this.flavors.size(); i++) {
            this.flavors.get(i).setDishId(dishId);
            this.flavors.get(i).setCreateTime(LocalDateTime.now());
            this.flavors.get(i).setUpdateTime(LocalDateTime.now());
            Long empId = (Long) request.getSession().getAttribute(LOGIN_SESSION);
            this.flavors.get(i).setCreateUser(empId);
            this.flavors.get(i).setUpdateUser(empId);
        }
    }

    public Dish updateDish(HttpServletRequest request) {
        Dish dish = new Dish();
        dish.setId(this.getId());
        dish.setName(this.getName());
        dish.setCategoryId(this.getCategoryId());
        dish.setPrice(this.getPrice());
        dish.setImage(this.getImage());
        dish.setDescription(this.getDescription());
        dish.setUpdateTime(LocalDateTime.now());
        Long empId = (Long) request.getSession().getAttribute(LOGIN_SESSION);
        dish.setUpdateUser(empId);
        return dish;
    }
}
